<template>
    <!-- 组件的模板 -->
    <div class="main">
        <div class="withdrawalway mg_rl12">
            <a href="javascript:;" v-for="(index,item) in unbindingList" :key="index">
                <img :src="item.bg_img">
                <div class="withdrawal_list">
                    <div class="list_left clfff">
                        <p class="ft18">{{item.account_name}}</p> 
                        <p class="ft12">{{item.account_note}}</p>   
                    </div>
                    <div class="list_right">
                        <i class="remove_binding db thinner-border br_2 ft12 clfff" @click="removeBingding(item.account_tag,item.account_name)">解绑</i>
                    </div>
                </div>
            </a>
        </div>
    </div>
        
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */

    import * as util from 'js/util.js'
    import widget from 'gapi/widget'
    import commonPage from 'clientApi/commonPage.js'
    import cache from 'gapi/cache.js'
    import safety from 'gapi/safety.js'
    
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                unbindingList:[],
                bindingList:[]
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                cache.getCache('WebViewID',(data)=>{
                    this.WebViewID = data;
                    cache.getCache('bind_list',(data)=>{
                        this.bindingList = data;
                        for(let i = 0, lg = data.length;i < lg; i++){
                            if(data[i].is_bind === 1){
                                data[i].bg_img = require('../../static/imgs/bj_' + data[i].account_tag +'.png')
                                this.unbindingList.push(data[i])
                            }
                        };
                        if(this.unbindingList.length < 0) location.go(-1)
                    })
                })
                                
            },
            removeBingding(type,name){
                weui.confirm('是否解绑？', {
                    title: '解绑'+ name +'账户',
                    className:'md_confirm',
                    buttons: [{
                        label: '取消',
                        type: 'default',
                        onClick: function(){ console.log('no') }
                    }, {
                        label: '解绑',
                        type: 'primary',
                        onClick: () =>{
                            this.unBinding(type)
                        }
                    }]
                });
                let confirmAndroidCss = document.querySelector('.weui-skin_android');
                if(confirmAndroidCss){
                    confirmAndroidCss.className = confirmAndroidCss.className.replace('weui-skin_android','')
                }
            },
            unBinding(bindtype){
                let  type,unBindType = bindtype,subData = {};
                switch(unBindType){
                    case 'bank':
                        safety.dekEncrypt('bank',(str) => {
                            type = str;
                            this.unBindSubmit(type,unBindType)
                        });
                    break;
                    case 'wechat':
                        safety.dekEncrypt('wechat',(str) => {
                            type = str;
                            this.unBindSubmit(type,unBindType)
                        });
                        break;
                    case 'alipay' :
                        safety.dekEncrypt('alipay',(str) => {
                            type = str;
                            this.unBindSubmit(type,unBindType)
                        });
                        break;
                }
            },
            unBindSubmit(type,unBindType){
                util.ajax({
                    url:'/wallet/bind_account',
                    type:'post',
                    timeout:10000,
                    data:{
                        operation:'un_bind',
                        type:type,
                        nickname:'',
                        openid:'',
                        true_name:'',
                        account:'',
                        card_number:'',
                        bank_name:''
                    },
                    success:(data) => {
                        //alert(JSON.stringify(data))                                        
                        widget.toast(data.message)
                        if(data.code === 901){
                            let bindData = this.bindingList;
                            for(let i = 0, lg = bindData.length;i < lg; i++){
                                if(this.unbindingList[i] && this.unbindingList[i].account_tag === unBindType){
                                    this.unbindingList.splice(i,1)
                                }
                                if(bindData[i].account_tag === unBindType){
                                    bindData[i].is_last_use = 0;
                                    bindData[i].is_bind = 0;
                                    if(bindData[i].account_tag === 'bank'){
                                        bindData[i].account_name = '银行卡'
                                    }
                                    break;
                                }                            
                            }
                            cache.deleteCache('bind_list');
                            cache.setCache('bind_list',bindData,10000)
                            if(this.unbindingList.length === 0){
                                commonPage.goBack(1,{},this.WebViewID);
                            }
                        }
                    },
                    error:()=>{
                        widget.toast('加载失败请稍后重试')
                    }
                })
            }
        },
        created(){
            commonPage.updateTitleBar({
                backAction: 'gotoback',
                leftAction: 'gotoback',
                rightDisplay: true,
                rightText:'完成',
                rightAction: 'gotoback'  
            })
            window.gotoback = () => {
                commonPage.goBack(1,{},this.WebViewID);
            }
            this.init();
            
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<!-- <style src="static/css/global.css"></style> -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/base.css"></style>
<style src="../../static/css/yy_editWithdrawal.css"></style>



